package br.com.duduso.conferencia_claro_desktop.screens;

import br.com.duduso.conferencia_claro_desktop.components.BarraProgresso;
import br.com.duduso.conferencia_claro_desktop.components.FrameInterno;
import br.com.duduso.conferencia_claro_desktop.components.Mensagens;
import br.com.duduso.conferencia_claro_desktop.components.SelecaoArquivo;
import br.com.duduso.conferencia_claro_desktop.dbf.ConexaoDBF;
import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author duduso
 */
public class TelaGeracaoDBF extends FrameInterno {

    private File arquivoDBF;
    private File arquivoXLS;  
    private static final String TEMP_DIR = System.getProperty("java.io.tmpdir") + File.separatorChar;
    private static final String ARQUIVO_SAIDA = TEMP_DIR + "Clientes_Ativos.dbf";
            
    public TelaGeracaoDBF() {
            super();
            initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tfArquivoDBF = new javax.swing.JTextField();
        btSelecionarArquivoDBF = new javax.swing.JButton();
        btSelecionarPlanilhaClientes = new javax.swing.JButton();
        tfPlanilhaClientes = new javax.swing.JTextField();
        lbArquivoDBF = new javax.swing.JLabel();
        lbPlanilhaClientes = new javax.swing.JLabel();
        btGerarDBF = new javax.swing.JButton();
        btFechar = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(952, 564));

        jPanel1.setBackground(new java.awt.Color(193, 205, 205));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "DADOS PRINCIPAIS", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        tfArquivoDBF.setEditable(false);
        tfArquivoDBF.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        tfArquivoDBF.setDisabledTextColor(new java.awt.Color(5, 5, 5));
        tfArquivoDBF.setEnabled(false);

        btSelecionarArquivoDBF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/dbase.png"))); // NOI18N
        btSelecionarArquivoDBF.setText("<html><b>Selecionar</b></html>");
        btSelecionarArquivoDBF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSelecionarArquivoDBFActionPerformed(evt);
            }
        });

        btSelecionarPlanilhaClientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/excel.png"))); // NOI18N
        btSelecionarPlanilhaClientes.setText("<html><b>Selecionar</b></html>");
        btSelecionarPlanilhaClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSelecionarPlanilhaClientesActionPerformed(evt);
            }
        });

        tfPlanilhaClientes.setEditable(false);
        tfPlanilhaClientes.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        tfPlanilhaClientes.setDisabledTextColor(new java.awt.Color(5, 5, 5));
        tfPlanilhaClientes.setEnabled(false);

        lbArquivoDBF.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        lbArquivoDBF.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbArquivoDBF.setText("Arquivo DBF:");

        lbPlanilhaClientes.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        lbPlanilhaClientes.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbPlanilhaClientes.setText("Planilha de Clientes (xls):");

        btGerarDBF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/conferir.png"))); // NOI18N
        btGerarDBF.setText("<html><b>Gerar Arquivo</b></html>");
        btGerarDBF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGerarDBFActionPerformed(evt);
            }
        });

        btFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/close.png"))); // NOI18N
        btFechar.setText("<html><b>Fechar</b></html>");
        btFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btGerarDBF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btFechar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lbArquivoDBF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbPlanilhaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfArquivoDBF, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                            .addComponent(tfPlanilhaClientes))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btSelecionarArquivoDBF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btSelecionarPlanilhaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(134, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbArquivoDBF)
                    .addComponent(tfArquivoDBF, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSelecionarArquivoDBF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPlanilhaClientes)
                    .addComponent(tfPlanilhaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSelecionarPlanilhaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 394, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btGerarDBF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btFechar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSelecionarArquivoDBFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelecionarArquivoDBFActionPerformed
            arquivoDBF = SelecaoArquivo.getInstance(".dbf").selecionarArquivo();
            if(arquivoDBF != null){
                    tfArquivoDBF.setText(arquivoDBF.getName());
            }else{
                    tfArquivoDBF.setText(null);
            }
    }//GEN-LAST:event_btSelecionarArquivoDBFActionPerformed

    private void btSelecionarPlanilhaClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelecionarPlanilhaClientesActionPerformed
            arquivoXLS = SelecaoArquivo.getInstance(".xls").selecionarArquivo();
            if(arquivoXLS != null){
                    tfPlanilhaClientes.setText(arquivoXLS.getName());
            }else{
                    tfPlanilhaClientes.setText(null);
            }
    }//GEN-LAST:event_btSelecionarPlanilhaClientesActionPerformed

    private void btGerarDBFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGerarDBFActionPerformed
            gerarArquivoDBF();
    }//GEN-LAST:event_btGerarDBFActionPerformed

    private void btFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFecharActionPerformed
            fecharTab();
    }//GEN-LAST:event_btFecharActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btFechar;
    private javax.swing.JButton btGerarDBF;
    private javax.swing.JButton btSelecionarArquivoDBF;
    private javax.swing.JButton btSelecionarPlanilhaClientes;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbArquivoDBF;
    private javax.swing.JLabel lbPlanilhaClientes;
    private javax.swing.JTextField tfArquivoDBF;
    private javax.swing.JTextField tfPlanilhaClientes;
    // End of variables declaration//GEN-END:variables

    
    private void gerarArquivoDBF(){
        
            if(this.arquivoDBF == null){
                    Mensagens.getInstance().exibirMensagemAviso(null, "geracaoDBFSelecioneArquivoDBF");
                    return;
            }
            
            if(this.arquivoXLS == null){
                    Mensagens.getInstance().exibirMensagemAviso(null, "geracaoDBFSelecioneArquivoXLS");
                    return;
            }
            
            new BarraProgresso() {
                    @Override
                    public void executar() {
                            try {
                                    ConexaoDBF.gerarDBFPadrao(arquivoXLS, arquivoDBF, ARQUIVO_SAIDA);
                                    Mensagens.getInstance().exibirMensagemInformacao(null, "msgArquivoDBFGeradoSucesso");
                            } catch (Exception ex) {
                                    JOptionPane.showMessageDialog(null, ex);
                            }
                    }
            };
        
    }
    
}
